package com.chenglu.android.mine.address.mvp.presenter;





import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.address.mvp.contract.AddressManagerContract;
import com.chenglu.android.mine.address.mvp.model.AddressManagerModel;
import com.chenglu.android.mine.bean.AddressListBean;
import com.chenglu.android.mine.bean.AddressSubmitBean;
import com.chenglu.android.mine.bean.AddressTreeBean;
import com.chenglu.android.mine.bean.CommonDataBean;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/9/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class AddressManagerPresenter extends BasePresenter<AddressManagerContract.View> implements AddressManagerContract.AddressManagerPresenter {

     private  AddressManagerContract.AddressManagerModel model;

    public AddressManagerPresenter(){
        model = new AddressManagerModel();
    }

    @Override
    public void getAddressList(String token) {

        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getAddressList(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AddressListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull AddressListBean bean) {
                        LogUtils.d("ddd----");
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:

                                mView.getAddressList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                         LogUtils.v(e.getMessage());
                        NetErrorException.apiError(e,mView);
                          mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    @Override
    public void getAddressSubmit(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getAddressSubmit(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AddressSubmitBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull AddressSubmitBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getAddressSubmit(bean);
                                break;
                            case Constant.HTTPCODE400:
//                                ToastUtils.show("请选择所在地区");
                                LogUtils.v(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
//                                ToastUtils.show("请选择所在地区");
                                LogUtils.v(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
//                                ToastUtils.show("请选择所在地区");
                                LogUtils.v(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
//                                ToastUtils.show("请选择所在地区");
                                LogUtils.v(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v(e.getMessage());
                        NetErrorException.apiError(e,mView);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    @Override
    public void getAddressUpdate(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getAddressUpdate(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AddressSubmitBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull AddressSubmitBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getAddressSubmit(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                LogUtils.v(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v(e.getMessage());
                        NetErrorException.apiError(e,mView);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getAddressTree() {
        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getAddressTree()
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AddressTreeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
//                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull AddressTreeBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getAddressTree(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                LogUtils.v(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v("address---",e.getMessage());
                        NetErrorException.apiError(e,mView);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    @Override
    public void getAddressDelete(String token,String ids) {
        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getAddressDelete(token,ids)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CommonDataBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CommonDataBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getAddressDelete(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v("address---",e.getMessage());
                        NetErrorException.apiError(e,mView);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


}
